package com.edu.service.impl;
/*
 * @Author:princess
 * @Date 2023/11/9 11 09 10:12
 *
 */

import com.edu.bean.Book;
import com.edu.bean.User;
import com.edu.mapper.BookMapper;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private BookMapper bookMapper;
    @Override
    public Map<String, Object> login(User user) {
        Map<String, Object> map = new HashMap<>();
        User loginUser = userMapper.selectByNameAndPass(user);
        if (loginUser != null) {
            map.put("loginUser", loginUser);
            List<Book> books = bookMapper.selectAllBooks();
            map.put("books",books);
        } else {
            loginUser = userMapper.selectByName(user.getUsername());
            if (loginUser == null) {
                map.put("msg", "用户名不存在");
            } else {
                map.put("msg", "密码错误");
            }
        }
        return map;
    }

    @Override
    public int register(User user) {
        User registerUser = userMapper.selectByName(user.getUsername());
        if (registerUser == null) {
            userMapper.register(user);
            return 1;
        } else {
            return -1;
        }
    }
}
